<template>
  <div class="app-container">
    <el-card>
      <div slot="header" class="clearfix">
        <span>服务记录列表</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="handleAdd">添加服务记录</el-button>
      </div>

      <el-table :data="list" border style="width: 100%">
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="serviceType" label="服务类型"></el-table-column>
        <el-table-column prop="serviceTime" label="服务时间"></el-table-column>
        <el-table-column label="参与员工">
          <template slot-scope="scope">
            <el-tag v-for="employee in scope.row.employees" :key="employee.id" style="margin-right: 5px;">
              {{ employee.name }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleDetail(scope.row.id)">详情</el-button>
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="50%">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="服务类型" prop="serviceType">
          <el-input v-model="form.serviceType"></el-input>
        </el-form-item>
        <el-form-item label="服务时间" prop="serviceTime">
          <el-date-picker
            v-model="form.serviceTime"
            type="datetime"
            placeholder="选择日期时间"
            value-format="yyyy-MM-dd HH:mm:ss">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input type="textarea" v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="参与员工" prop="employeeIds">
          <el-select v-model="form.employeeIds" multiple placeholder="请选择员工">
            <el-option
              v-for="employee in employeeList"
              :key="employee.id"
              :label="employee.name"
              :value="employee.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="服务记录详情" :visible.sync="detailVisible" width="60%">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="服务类型">{{ currentRecord.serviceType }}</el-descriptions-item>
        <el-descriptions-item label="服务时间">{{ currentRecord.serviceTime }}</el-descriptions-item>
        <el-descriptions-item label="描述">{{ currentRecord.description }}</el-descriptions-item>
      </el-descriptions>

      <el-divider></el-divider>
      <h4>参与员工</h4>
      <el-table :data="employeesInRecord" border style="width: 100%">
        <el-table-column prop="name" label="姓名"></el-table-column>
        <el-table-column prop="position" label="职位"></el-table-column>
        <el-table-column prop="department" label="部门"></el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { getServiceRecordList, addServiceRecord, updateServiceRecord, deleteServiceRecord, getServiceRecordDetail } from '@/api/serviceRecord'
import { getEmployeeList } from '@/api/employee'

export default {
  data() {
    return {
      list: [],
      employeeList: [],
      employeesInRecord: [],
      currentRecord: {},
      dialogVisible: false,
      detailVisible: false,
      dialogTitle: '',
      form: {
        id: null,
        serviceType: '',
        serviceTime: '',
        description: '',
        employeeIds: []
      },
      rules: {
        serviceType: [{ required: true, message: '请输入服务类型', trigger: 'blur' }],
        serviceTime: [{ required: true, message: '请选择服务时间', trigger: 'change' }]
      }
    }
  },
  created() {
    this.fetchData()
    this.fetchEmployeeList()
  },
  methods: {
    fetchData() {
      getServiceRecordList().then(response => {
        this.list = response.data
      })
    },
    fetchEmployeeList() {
      getEmployeeList().then(response => {
        this.employeeList = response.data
      })
    },
    handleAdd() {
      this.dialogTitle = '添加服务记录'
      this.form = {
        id: null,
        serviceType: '',
        serviceTime: '',
        description: '',
        employeeIds: []
      }
      this.dialogVisible = true
    },
    handleEdit(row) {
      this.dialogTitle = '编辑服务记录'
      this.form = Object.assign({}, row)
      this.form.employeeIds = row.employees.map(e => e.id)
      this.dialogVisible = true
    },
    handleDelete(id) {
      this.$confirm('确认删除该服务记录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteServiceRecord(id).then(() => {
          this.$message.success('删除成功')
          this.fetchData()
        })
      })
    },
    handleDetail(id) {
      getServiceRecordDetail(id).then(response => {
        this.currentRecord = response.data
        this.employeesInRecord = response.data.employees
        this.detailVisible = true
      })
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.id) {
            updateServiceRecord(this.form).then(() => {
              this.$message.success('更新成功')
              this.dialogVisible = false
              this.fetchData()
            })
          } else {
            addServiceRecord(this.form, this.form.employeeIds).then(() => {
              this.$message.success('添加成功')
              this.dialogVisible = false
              this.fetchData()
            })
          }
        }
      })
    }
  }
}
</script>
